/*
 * @Description:
 * @Author: lixx
 * @Date: 2025-09-24 10:19:53
 * @LastEditTime: 2025-09-25 17:10:02
 * @LastEditors: lixx
 */
import { createApp } from "vue";
import App from "./App.vue";
import "@/assets/less/common.less";
import router from "./router";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import { createPinia } from "pinia";
import "@/api/mock/index.js";
import { useAllDateStore } from "@/stores";
// import api from "@/api/home.js";

const pinia = createPinia();
let app = createApp(App);
// app.config.globalProperties.$api = api
app.use(pinia);  // 必须在 mount 之前使用 pinia
app.use(ElementPlus);
app.use(router);

// 注册所有图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

// 在挂载前处理动态路由
const store = useAllDateStore();
store.addDynamicRouter(router,'refresh');

// 路由守卫
router.beforeEach((to, from, next) => {
  // 白名单：不需要token就能访问的页面
  const whiteList = ['/login', '/404']
  
  // 如果没有token且不在白名单中，跳转到登录页
  if(!whiteList.includes(to.path) && !store.state.value.token){
    next('/login')
  }else{
    next()
  }
})

app.mount("#app");
